package com.lsl.test1;

import java.util.List;

/**
 * @author lishuailong
 * @create 2020-06-15 16:42
 */
public class TestB {
    public static void main(String[] args) {
        Window window1= new Window();
        window1.setName("窗口1");
        Window window2= new Window();
        window2.setName("窗口2");
        Window window3= new Window();
        window3.setName("窗口3");

        window1.start();
        window2.start();
        window3.start();

    }
}



class Window extends Thread{
    //当前票数
    private static int tickets =30;

    @Override
    public void run() {
        while (true){

            if (ticket()) break;


        }

    }

    private static synchronized boolean ticket() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (tickets<1){
            return true;
        }else {
            System.out.println(Thread.currentThread().getName()+"卖出了"+tickets+"号票！");
            tickets--;
        }
        return false;
    }
}
